package gui.action;

import generated.business.avatar.AvatarServicePackage.Mood;
import gui.Activator;
import gui.IImageKeys;
import gui.ResourceManager;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

import vworld.Client;

public class MoodAction extends Action {

	public final static String ID = "gui.action.MoodAction";

	public MoodAction(String text) {
		super(text, IAction.AS_DROP_DOWN_MENU);

		this.setMenuCreator(new IMenuCreator() {
			public Menu getMenu(Control control) {
				Menu menu = new Menu(control);
				MenuItem elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("happy");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.HAPPY));
				elem.setEnabled(!isCurrentMood("happy"));
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						run("happy");
					}

				});

				elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("laughing");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.LAUGHING));
				elem.setEnabled(!isCurrentMood("laughing"));
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						run("laughing");
					}

				});

				elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("sad");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.SAD));
				elem.setEnabled(!isCurrentMood("sad"));
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						run("sad");
					}
				});

				elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("worried");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.WORRIED));
				elem.setEnabled(!isCurrentMood("worried"));
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						run("worried");
					}
				});

				elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("scared");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.SCARED));
				elem.setEnabled(!isCurrentMood("scared"));
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						run("scared");
					}
				});

				control.setMenu(menu);
				return menu;
			}

			private boolean isCurrentMood(String mood) {
				return Client.CONNECTED_USER.avatar.mood.equalsIgnoreCase(mood);
			}

			public Menu getMenu(Menu menu) {
				return menu;
			}

			public void dispose() {
			}
		});
	}

	public void run(String _mood) {
		Client.CONNECTED_USER.avatar.mood = _mood;
		Mood currentAvatarMood = Mood.from_int(vworld.adapter.MoodAdapter
				.intMoodValue(_mood));
		Client.AVATAR_SERVICE.changeMood(Client.CALL_BACK_REF,
				Client.CONNECTED_USER, currentAvatarMood);
	}
}
